import router from "../router";
import { useBreadCrumb } from "../store/breadCrumb"
import { usePermission } from "../store/permission"

router.beforeEach(async (to, from) => {
    let permissStore = usePermission()
    // console.log(to)
    // console.log(from)
    //判断本地的sess ionSto rage中是否有token,如果有,啥也不干
    let token = sessionStorage.getItem('pbz-token')
    if (!token) {
        if (to.path !== '/login') {
            return {
                path: '/login'
            }
        }
    } else {
        //如果menulist的长度是0那么就要发起请求
        if (permissStore.menulist.length === 0) {
            // 调用store中的action方法发起异步请求
            await permissStore.fetch_menulist()

            return {
                ...to
            }
        }
        

    }
})


// 全局后置钩子


router.afterEach((to, from) => {
    
    let breadCrumbSrore = useBreadCrumb()
    breadCrumbSrore.$patch(state => {
        state.datas = to.matched
    })
}) 